<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Ads extends Admin_Controller {

	public $model = "ads";
	public $view = "ads";
	public $permissionCreate = 'Ads.Create';
    public $permissionDelete = 'Ads.Delete';
    public $permissionEdit   = 'Ads.Edit';
    public $permissionView   = 'Ads.View';
	
	protected $index_show = array(
		array( 'field' => 'ads_id',
				'label' => 'Ads ID'),	
		array( 'field' => 'ads_name',
				'label' => 'Ads Name',
				'custom_value' => '<a href="{url_edit}{ads_id}">{ads_name}</a>'//replace value
				),
				array( 'field' => 'ads_time_start',
				'label' => 'Ngày tạo',
				'custom_value' => '<a href="{url_edit}{ads_id}">{ads_time_start}</a>'
				),
		//array( 'field' => 'mpos_id',
		//		'label' => 'permission description'),
	);
    function __construct() {
        parent::__construct();
        $this->load->model($this->model.'_m',$this->model);
		$this->_model = $this->{$this->model};

    }
	
	function ajax_upload($info_id = 0){
		require(APPPATH.'libraries/FileUpload.php');
		$uploader = new FileUpload('info_thumb');
		$this->folder_upload = "images/";
		if($info_id <=0) die();
		if(!is_dir($this->folder_upload))
		mkdir($this->folder_upload,0777);
		$uploader->allowedExtensions = array('jpg', 'jpeg', 'png', 'gif','swf');
		$uploader->sizeLimit = 91071520;
		$uploader->newFileName = 'product-'.$info_id.'.'.$uploader->getExtension();
		$result = $uploader->handleUpload('./'.$this->folder_upload);
		if (!$result) {
			echo json_encode(array(
			'success' => false,
			'msg' => $uploader->getErrorMsg()
			));    
			
		} else {
			if(!is_array($info_id)) {
				/*
				$this->{$this->model}->update($info_id,'product_id', array(
				'product_thumb' => $this->folder_upload.''.$uploader->getFileName(),
				));
				*/
			}
			echo json_encode(array(
			'success' => true,
			'file' => base_url().$this->folder_upload.''.$uploader->getFileName()."?v=".time(),
			));
			
		}
	}
}
